package com.xyd.servlet;

import com.xyd.entity.Message;
import com.xyd.entity.Product;
import com.xyd.entity.User;
import com.xyd.service.CartService;
import com.xyd.service.ProductSericve;
import com.xyd.service.impl.CartServiceImpl;
import com.xyd.service.impl.ProductServiceImpl;
import com.xyd.util.CookieUtil;
import com.xyd.util.JsonUtil;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/13 14:22
 */
@WebServlet("/cartAddServlet")
public class CartAddServlet extends HttpServlet {

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    User user = (User) request.getSession().getAttribute("user");
    Message message = new Message();
    if (user == null) {
      message.setFlag(false);
      message.setMessage("您尚未登陆，请登陆后再试");
      JsonUtil.toJson(response,message);
      return;
    }

    Long epId = Long.parseLong(request.getParameter("epId"));
    ProductSericve productSericve = new ProductServiceImpl();
    Product product = productSericve.getProductByEpid1(epId);

    CartService cartService = new CartServiceImpl();

    Cookie[] cookies = request.getCookies();
    boolean flag = true;
    for (Cookie cookie : cookies) {
      if ((user.getEuUserId() + CookieUtil.getCookieName()).equals(cookie.getName())) {
        // 说明已有的cookies中有相应的cookie，就进行更新操作
        String oldValue = cookie.getValue();
        StringBuilder newValue = new StringBuilder();
        if (oldValue.length() > 0) {
          String[] productIds = oldValue.split("\\*");
          for (String productId : productIds) {
            String[] split = productId.split(",");
            if (flag && split[0].equals(product.getEpId().toString())) {
              boolean b = cartService.updateOneProduct(product.getEpId(), user.getEuUserId());
              message.setFlag(b);
              split[1] = "" + (Integer.parseInt(split[1]) + 1);
              flag = false;
            }
            newValue.append(split[0]).append(",").append(split[1]).append("*");
          }
        }
        if (flag) {
          newValue.append(epId).append(",1*");
          cartService.addProduct(product.getEpId(), user.getEuUserId());
        }
        CookieUtil.fixCookie(cookie, newValue.toString(), response);
        flag = false;
        break;
      }
    }

    if (flag) {
      // 已有的cookies中没有相应的cookie，进行添加
      CookieUtil.saveCookie(product.getEpId() + ",1*", user.getEuUserId(), response);
      Boolean b = cartService.addProduct(product.getEpId(), user.getEuUserId());
      message.setFlag(b);
    }
    JsonUtil.toJson(response, message);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doPost(request, response);
  }
}
